<?php
/**
 * 货柜
 */

namespace App\Admin\Controllers\Products;


use App\Admin\Extensions\Grid\Tool\Import;
use App\Models\Product\ProductMaterialContainer;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class MaterialContainerController extends AdminController{

    protected $title = '材料货柜';

    protected function grid(){

        $grid = new Grid(new ProductMaterialContainer());
        $grid->column('id', 'ID');
        $grid->column('code', '货柜代码');
        $grid->column('type', '货柜类型')->using(ProductMaterialContainer::$_type);
        $grid->column('title', '货柜名称');
        //$grid->column('操作')->view('products.material-container.action');

        //$grid->tools(function (Grid\Tools $tools) {
        //    $tools->append(new Import('products/material-container-upload?type=container'));
        //});

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            //if(!Admin::user()->isRole('administrator') || !Admin::user()->isRole('qe')){
            //    $actions->disableEdit(); //禁用编辑
            //}
            $actions->disableView();
            $actions->disableDelete();
        });
        //if(!Admin::user()->isRole('administrator') || !Admin::user()->isRole('qe')){
        //    $grid->disableCreateButton(); //禁用添加
        //}

        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->expandFilter();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->where(function ($query) {
                    $query->where('code', 'like', "%{$this->input}%")
                        ->orWhere('title', 'like', "%{$this->input}%");
                }, '查询','search8')->placeholder('模糊查询货柜代码、名称');
            });
            $filter->column(1/3, function ($filter){
                $filter->equal('type', '货柜类型')->select(ProductMaterialContainer::$_type);
            });
        });

        return $grid;
    }

    public function form(){

        $form = new Form(new ProductMaterialContainer());
        $form->hidden('id');
        $form->hidden('code');
        $form->select('type','货柜类型')->options(ProductMaterialContainer::$_type)->rules('required')->help('选择货柜类型');
        $form->text('title','货柜名称')->rules('required')->help('输入货柜名称');
        $form->hidden('admin_id')->default(Admin::user()->id);

        $form->saving(function (Form $form) {
            if ($form->isCreating()) {
                /*****************柜号代码 start**************************/
                $code = self::getCode($form->type); //货柜代码
                $existCode = ProductMaterialContainer::getValueByCode($code,'id');
                if ($existCode) {
                    admin_error('ERROR', '货柜代码已存在，请重新设置编码规则');
                    return back()->withInput();
                }
                /*****************柜号代码 end**************************/
                $form->code = $code;
            }
        });

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });

        return $form;
    }

    /**
     * 货柜编码 (自动生成)
     * @param $type
     * @return string
     */
    public static function getCode($type){
        $codeStr = ProductMaterialContainer::$_type_string[$type]; //例：上海货柜缩写为SH，深圳货柜缩写为SZ
        $materialCode = ProductMaterialContainer::query()->where('type', $type)->max('code'); //获取类型下的最大编码
        if ($materialCode) {
            $lastNumber = intval(substr($materialCode, -4)) + 1; //尾数累计加1)
        }else{
            $lastNumber = 1; //尾数初始值(从1开始累计)
        }
        $codeNumber = str_pad($lastNumber, 4, '0', STR_PAD_LEFT); //尾数，后四位（补位填充数字）

        return $codeStr.'_'.$codeNumber; //货柜编码(SH_0001)
    }

}
